const app = getApp()
import areaList from '../../config/area.js';
import {
  masterList,
  workCate,
} from '../../config/https'
//同程师傅列表、获取工种
Page({
  /**
   * 页面的初始数据
   */
  data: {
    longitude: '',               //经度
    latitude: '',                //纬度
    masterType: '',              //同程师傅 不用传（精英师傅 传 这个）
    provinceShow: false,
    areaList: areaList,
    address: '',                  //地址
    city: '请选择',
    adcode: '',
    maskShow: false,
    chooseType: '',               //1为工种2位默认
    typeVal: {                    //工种
      name: '工种',
      id: ''
    },              
    defaultVal: {                 //默认
      name: '默认',
      id: ''
    },        
    workType: '',                 //工种类型
    workTypeVal: '',              //工种类型
    chkType: ['默认', '成交量', '好评分'],
    columns: '',
    masterList: [],                //师傅列表
    page: 0,
    pagesize: 10
  },
  goDetails(e) {
    wx.navigateTo({
      url: '/pages/workDetails/workDetails?id=' + e.currentTarget.dataset.id,
    })
  },
  //省市弹窗
  getProvince() {
    console.log(app.globalData.adcode)
    this.setData({
      adcode: app.globalData.adcode,
      provinceShow: true
    })
  },
  //关闭省市弹窗
  provinceClose() {
    this.setData({
      provinceShow: false
    })
  },
  //获取省市
  confirmProvince(e) {
    let city = e.detail.values[1].name
    let selectVal = e.detail.values[0].name + e.detail.values[1].name
    this.setData({
      city,
      address: selectVal,
      page: 0,
      masterList: []
    })
    app.getcoordinate(selectVal).then(res => {
      this.setData({
        longitude: res.location.lng,        //经度
        latitude: res.location.lat          //纬度
      })
      this.getData()
    })
    this.provinceClose()
  },
  //工种弹窗 || 默认弹窗
  showMask(e) {
    let type = e.currentTarget.dataset.type
    if (type == 1) {
      this.setData({
        columns: this.data.workTypeVal,
        chooseType: type
      })
    } else {
      this.setData({
        columns: this.data.chkType,
        chooseType: type
      })
    }
    this.setData({
      maskShow: true
    })
  },
  //关闭工种弹窗 || 关闭默认弹窗
  onClose() {
    this.setData({
      maskShow: false
    })
  },
  //获取工种
  getWorkType() {
    workCate().then(res => {
      let workType = res.data
      let newArr = workType.map(item => {
        return item.name
      })
      newArr.unshift('全部')
      workType.unshift({id: '',name: '全部'})
      this.setData({
        workTypeVal: newArr,
        workType
      })
    })
  },
  //获取选中弹窗值
  confirmClick(e) {
    let chooseType = this.data.chooseType
    if (chooseType == 1) {
      let id = this.data.workType[e.detail.index].id //工种id
      this.setData({
        typeVal: {
          'name': e.detail.value,
          'id': id
        }
      })
    } else {
      let id = ''
      e.detail.index == 0 ? id = '' : id = e.detail.id
      this.setData({
        defaultVal: {
          'name': e.detail.value,
          'id': id 
        }
      })
    }
    this.setData({
      page: 0,
      masterList: []
    })
    this.getData()
    this.onClose()
  },
  getData(){
    masterList({
      longitude: this.data.longitude,
      latitude: this.data.latitude,
      address: this.data.address,
      work_cate_id: this.data.typeVal.id,
      type: this.data.defaultVal.id,
      page: ++this.data.page,
      pagesize: this.data.pagesize,
      master_type: this.data.masterType ? this.data.masterType : ''
    }).then(res => {
      if(res.code == 1){
        this.setData({
          masterList: [...this.data.masterList,...res.data]
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let title = options.masterType == 2 ? '精英师傅' : '同城师傅'
    wx.setNavigationBarTitle({
      title
    })
    this.setData({
      masterType: options.masterType == 1 ? '' : options.masterType
    })
    this.getWorkType()  //获取工种
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!app.globalData.chkLocation) {
      app.location().then(res => {
        this.setData({
          address: res.address,             
          city: res.address_component.city,
          longitude: res.location.lng,    //经度
          latitude: res.location.lat      //纬度
        })
        this.getData()
      }).catch(err => {
        console.log(err)
        wx.navigateBack()
      })
    }else{
      this.setData({
        address: app.globalData.address,          //地址
        city: app.globalData.city,                //市
        longitude: app.globalData.longitude,      //经度
        latitude: app.globalData.latitude         //纬度
      })
      this.getData()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getData()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})